﻿using System;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopB2C.Core.Entities;
using ShopB2C.Core.Enums;

namespace ShopB2C.Core.Helpers {
    /// <summary>
    /// 页码生成控件，一直显示10个号码
    /// </summary>
    internal class ControlPagerHelper : CompositeControl {

        #region Fileds

        private HtmlTextWriter _writer;
        private PagerParams _params;
        private int _pageCount;
        private int _totalRows;
        private string _hrefFormat;
        private int _pageIndex;
        private int _pageSize;
        private string _lastPageText;
        private string _firstPageText;
        private string _pageUpText;
        private string _pageDownText;

        #endregion

        /// <summary>
        /// 分页参数名称
        /// </summary>
        public PagerParams PagerParams {
            get { return this._params; }
            set {
                this._params = value;
                this._totalRows = this._params.RecordCount;
                this._hrefFormat = this._params.Url;
                this._pageIndex = this._params.PageIndex;
                this._pageSize = this._params.PageSize;
                this._lastPageText = this._params.Language[PagerResourceKey.Last];
                this._firstPageText = this._params.Language[PagerResourceKey.First];
                this._pageUpText = this._params.Language[PagerResourceKey.Previous];
                this._pageDownText = this._params.Language[PagerResourceKey.Next];
                this._writer = new HtmlTextWriter(this._params.TextWriter);
                this.CalculatePageCount();
                this.CreateUrl();
            }
        }
        /// <summary>
        /// 默认呈现标记
        /// </summary>
        protected override HtmlTextWriterTag TagKey {
            get { return HtmlTextWriterTag.Div; }
        }
        /// <summary>
        /// 添加 CSS 样式
        /// </summary>
        /// <param name="writer"></param>
        protected override void AddAttributesToRender(HtmlTextWriter writer) {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "shopb2c_core_pager_2");
            base.AddAttributesToRender(writer);
        }
        /// <summary>
        /// 释放
        /// </summary>
        public override void Dispose() {
            this._writer.Close();
            this._writer.Dispose();
            base.Dispose();
        }
        /// <summary>
        /// 创建子控件
        /// </summary>
        protected override void CreateChildControls() {
            Controls.Clear();

            int starNumber = 1, endNumber = _pageCount;
            // 一直显示 10 个号码
            if (_pageIndex > 4) {
                endNumber = _pageIndex + 5 < _pageCount ? _pageIndex + 5 : _pageCount;
            }
            else {
                endNumber = _pageCount > 10 ? 10 : _pageCount;
            }

            starNumber = endNumber - 9 > 0 ? endNumber - 9 : 1;
            Label l = new Label();
            if (this._params.PagerTotal == PagerTotal.LongTotal) {
                l.Text = string.Format(
                        this._params.Language[PagerResourceKey.LongTotal],
                        this._totalRows,
                        this._pageCount,
                        this._pageIndex,
                        this._pageSize);
            }
            else {
                l.Text = string.Format(
                        this._params.Language[PagerResourceKey.ShortTotal],
                        this._totalRows,
                        this._pageIndex,
                        this._pageCount);
            }
            this.Controls.Add(l);

            // 第一页
            HyperLink firstPage = new HyperLink();
            firstPage.Text = _firstPageText;
            firstPage.NavigateUrl = _pageIndex > 1 ? string.Format(_hrefFormat, 1, this._pageSize) : null;
            if (_pageIndex <= 1) {
                firstPage.Attributes.Add("class", "disabled");
                firstPage.Attributes.CssStyle.Add(HtmlTextWriterStyle.MarginLeft, "0px");
            }
            this.Controls.Add(firstPage);

            // 上一页
            HyperLink previousPage = new HyperLink();
            previousPage.Text = _pageUpText;
            previousPage.NavigateUrl = _pageIndex > 1 ? string.Format(_hrefFormat, _pageIndex - 1, this._pageSize) : null;
            if (_pageIndex <= 1)
                previousPage.Attributes.Add("class", "disabled");
            this.Controls.Add(previousPage);
            // 开始编号
            for (int i = starNumber; i <= endNumber; i++) {
                HyperLink h = new HyperLink();
                h.Text = string.Format("{0}", i);
                h.NavigateUrl = string.Format(_hrefFormat, i, this._pageSize);
                // 当前页
                if (_pageIndex == i) {
                    h.Attributes.Add("class", "current-index");
                    h.NavigateUrl = null;
                }
                this.Controls.Add(h);
            }
            // 下一页
            HyperLink nextPage = new HyperLink();
            nextPage.Text = _pageDownText;
            nextPage.NavigateUrl = _pageIndex < _pageCount ? string.Format(_hrefFormat, _pageIndex + 1, this._pageSize) : null;
            if (_pageIndex >= _pageCount)
                nextPage.Attributes.Add("class", "disabled");
            this.Controls.Add(nextPage);
            // 最后一页
            HyperLink lastPage = new HyperLink();
            lastPage.Text = _lastPageText;
            lastPage.NavigateUrl = _pageIndex < _pageCount ? string.Format(_hrefFormat, _pageCount, this._pageSize) : null;
            if (_pageIndex >= _pageCount) {
                lastPage.Attributes.Add("class", "disabled");
                lastPage.Attributes.CssStyle.Add(HtmlTextWriterStyle.MarginRight, "0px");
            }
            this.Controls.Add(lastPage);

            ChildControlsCreated = true;
        }
        /// <summary>
        /// 执行
        /// </summary>
        public void Create() {
            this.CreateChildControls();
            this.Render(this._writer);
        }        
        // 总算总页数
        private void CalculatePageCount() {
            if (this._params.RecordCount <= 0 || this._params.PageSize <= 0) {
                this._pageCount = 1;
            }
            else {
                this._pageCount = this._params.RecordCount / this._params.PageSize;
                if (this._params.RecordCount % this._params.PageSize > 0) {
                    this._pageCount += 1;
                }
            }
        }
        // 组合 Url
        private void CreateUrl() {
            string separator = "?";
            if (this._params.Url.Contains("?")) {
                separator = "&";
            }
            this._hrefFormat += (separator + "p={0}&s={1}");
        }
    }
}
